<#assign content>
<div class="right_col" role="main">
    <div class="">
        <div class="page-title">
            <div class="title_left">
                <h3>机台列表</h3>
            </div>
            <div class="title_right">
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>
                            <small></small>
                        </h2>
                        <ul class="nav navbar-right panel_toolbox">
                            <li><a id="machineAdd"><i class="fa fa-plus-circle"></i></a></li>
                            <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                            </li>
                        </ul>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">
                        <p class="text-muted font-13 m-b-30">

                        </p>
                        <table id="machinetable" class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th>机台名</th>
                                <th>机台标识</th>
                                <th>摆放地址</th>
                                <th>纬度</th>
                                <th>经度</th>
                                <th>设备名</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>

                            <tfoot>
                            <tr>
                                <th>机台名</th>
                                <th>机台标识</th>
                                <th>摆放地址</th>
                                <th>纬度</th>
                                <th>经度</th>
                                <th>设备名</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</#assign>
<#include "../_inc/layout.ftl" />
<#--//机台新增-->
<div class="modal fade" id="machineAddModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">机台新增</h4>
            </div>
            <form id="machineAddForm" method="post" novalidate class="form-horizontal form-label-left">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-4">机台名<span class="required">*</span>
                        </label>
                        <div class="col-md-6">
                            <input type="text" id="addname" name="name"
                                   placeholder="请输入机台名" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-4">机台标识
                        </label>
                        <div class="col-md-6">
                            <input type="text" id="addsign" name="sign"
                                   placeholder="请输入机台标识" class="form-control col-md-6">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-4">摆放地址<span class="required">*</span>
                        </label>
                        <div class="col-md-6">
                            <input type="text" id="addaddress" name="address"
                                   placeholder="请输入地址" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-4">设备名<span class="required">*</span>
                        </label>
                        <div class="col-md-6">
                            <select name="did" id="adddid" class="form-control col-md-6" required>
                                <option value="">---请选择---</option>
                            </select>
                        </div>
                    </div>
                    <div class="ln_solid"></div>
                    <div class="form-group">
                        <div class="col-md-8 col-md-offset-9">
                            <button id="saveMachine" type="button" class="btn btn-success">保存</button>
                            <button class="btn btn-primary" type="reset">重置</button>
                        </div>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<#--//机台修改-->
<div class="modal fade" id="machineEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">机台修改</h4>
            </div>
            <form id="machineEditForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="edit_machineid" name="id">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-4">机台名
                        </label>
                        <div class="col-md-6">
                            <input type="text" id="editname" name="name"
                                   placeholder="请输入机台名" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-4">机台标识<span class="required">*</span>
                        </label>
                        <div class="col-md-6">
                            <input type="text" id="editsign" name="sign"
                                   placeholder="请输入机台标识" class="form-control col-md-6">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-4">摆放地址<span class="required">*</span>
                        </label>
                        <div class="col-md-6">
                            <input type="text" id="editaddress" name="address"
                                   placeholder="请输入地址" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-4">设备名<span class="required">*</span>
                        </label>
                        <div class="col-md-6">
                            <select name="did" id="editdevice" class="form-control col-md-6" required>
                                <option value="">---请选择---</option>
                            </select>
                        </div>
                    </div>
                    <div class="ln_solid"></div>
                    <div class="form-group">
                        <div class="col-md-8 col-md-offset-9">
                            <button id="saveEditMachine" type="button" class="btn btn-success">保存</button>
                            <button class="btn btn-primary" type="reset">重置</button>
                        </div>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<#--机台删除-->
<div class="modal fade" id="machineDeleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">机台删除</h4>
            </div>
            <form id="machineDeleteForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="delete_machineid" name="id">
                <div class="modal-body">
                    <div class="item form-group">
                        <div class="col-md-10">
                            <span id="machineDeleteMessage"></span>
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saveDeleteMachine" type="button" class="btn btn-success">确定</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<script>
    var machinetable;

    $(document).ready(function () {
        machinetable = $('#machinetable').DataTable({
            lengthChange: false,            // 是否允许用户自定义显示数量
            bPaginate: true,                // 翻页功能
            bFilter: false,                 // 列筛序功能
            ordering: false,                // 排序功能
            Info: true,                     // 页脚信息
            autoWidth: true,                // 自动宽度
            pageLength: 10,                 // 每页显示10条数据
            oLanguage: {                    // 国际语言转化
                oAria: {
                    sSortAscending: ': 以升序排列此列',
                    sSortDescending: ': 以降序排列此列'
                },
                sLengthMenu: '显示 _MENU_ 记录',
                sZeroRecords: '对不起，查询不到任何相关数据',
                sEmptyTable: '未有相关数据',
                sLoadingRecords: '正在加载数据...',
                sInfo: '当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录。',
                sInfoEmpty: '当前显示0到0条，共0条记录。',
                sSearch: '查询:',
                oPaginate: {
                    sFirst: '首页',
                    sPrevious: ' 上一页 ',
                    sNext: ' 下一页 ',
                    sLast: ' 尾页 '
                }
            },
            stripeClasses: ["odd", "even"],                         //为奇偶行加上样式，兼容不支持CSS伪类的场合
            paginationType: 'full_numbers',
            serverSide: true,
            ajax: function (data, callback, settings) {
                var param = {};                                     // 封装请求参数
                param.limit = data.length;                          // 页面显示记录条数，在页面显示每页显示多少项的时候
                param.start = data.start;                           // 开始的记录序号
                param.page = (data.start / data.length) + 1;        // 当前页码
                param.action = 'page';
                $.ajax({
                    type: 'POST',
                    url: '/machine_list.do',
                    cache: false,                                   //禁用缓存
                    data: param,                                    //传入组装的参数
                    dataType: 'json',
                    success: function (result) {
                        if (result['success']) {
                            // 封装返回数据
                            var returnData = {};
                            returnData.recordsTotal = result['recordsTotal'];              // 返回数据全部记录
                            returnData.recordsFiltered = result['recordsFiltered'];        // 后台不实现过滤功能，每次查询均视作全部结果
                            returnData.data = result['records'];                           // 返回的数据列表
                            callback(returnData);
                        } else {
                            $.promptModal('${warning}', result['msg'], result.logout);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            },
            columns: [
                {data: 'name'},
                {data: 'sign'},
                {data: 'address'},
                {data: 'latitude'},
                {data: 'longitude'},
                {data: 'devicename'},
                {
                    data: 'flag',
                    render: function (data, type, row, meta) {
                        if (data == 1) {
                            return '<span style="color: #00CC33">正常</span>';
                        } else {
                            return '<span style="color: #be2626">已删除</span>';
                        }
                    }
                },
                {
                    data: 'id',
                    render: function (data, type, row, meta) {
                        var str = '';
                        if (row.flag == 1) {
                            str += '<button onclick="$.editMachine(\'' + row.id + '\')" type="button" class="btn btn-success btn-xs"><i class="fa fa-pencil"></i>修改</button>';
                            str += '<button onclick="$.deleteMachine(\'' + row.id + '\')" type="button" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i>删除</button>';
                            str += '<button onclick="$.machinedetil(\'' + row.id + '\')" type="button" class="btn btn-info btn-xs"><i class="fa fa-plus-square"></i>参数明细</button>';
                        }
                        return str;
                    }
                }
            ],
            columnDefs: [
                {orderable: false, targets: '_all'},
                {defaultContent: '', targets: '_all'}
            ]
        });

        // 机台新增
        $('#machineAdd').on('click', function () {
            var param = {};
            $.ajax({
                type: 'POST',
                url: '/machine_add.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function(result) {
                    if(result.success) {
                        $('#adddid').html(result.body.deviceList);
                        $('#machineAddModal').modal({
                            keyboard: true
                        });
                    }
                    else {
                        $.promptModal('${warning}', result.body.msg, result.logout);
                    }

                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        });

        // 窗体关闭清空页面
        $('#machineAddModal').on('hide.bs.modal', function () {
            $('#machineAddForm')[0].reset();
        });

        // 提交保存
        $('#saveMachine').on('click', function () {
            if (validator.checkAll($('#machineAddForm'))) {
                var param = {};
                param.action = 'do';
                param.name = $('#addname').val();
                param.sign = $('#addsign').val();
                param.address = $('#addaddress').val();
                param.did = $('#adddid').val();
                $('#machineAddForm')[0].reset();
                $('#machineAddModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/machine_add.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function (result) {
                        if (result.success) {
                            machinetable.draw(false);
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });

        // 窗体关闭清空页面
        $('#machineEditModal').on('hide.bs.modal', function () {
            $('#machineEditForm')[0].reset();
        });

        // 修改
        $.editMachine = function (id) {
            var param = {};
            param.action = 'edit';
            param.id = id;
            $.ajax({
                type: 'POST',
                url: '/machine_edit.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        $('#edit_machineid').val(id);
                        $('#editname').val(result.body.sMachine.name);
                        $('#editsign').val(result.body.sMachine.sign);
                        $('#editaddress').val(result.body.sMachine.address);
                        $('#editdevice').html(result.body.deviceList);
                        $('#machineEditModal').modal({
                            keyboard: true
                        });
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        };

        // 保存修改
        $('#saveEditMachine').on('click', function () {
            if (validator.checkAll($('#machineEditForm'))) {
                var param = {};
                param.action = 'doEdit';
                param.id = $('#edit_machineid').val();
                param.sign = $('#editsign').val();
                param.name = $('#editname').val();
                param.address = $('#editaddress').val();
                param.editdevice = $('#editdevice').val();
                $('#machineEditForm')[0].reset();
                $('#machineEditModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/machine_edit.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function (result) {
                        if (result.success) {
                            machinetable.draw(false);
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });

        // 删除
        $.deleteMachine = function (id) {
            $('#delete_machineid').val(id);
            $('#machineDeleteMessage').html('确定删除机台？');
            $('#machineDeleteModal').modal({
                keyboard: true
            });
        };

        // 保存删除
        $('#saveDeleteMachine').on('click', function () {
            var param = {};
            param.action = 'doDelete';
            param.id = $('#delete_machineid').val();
            $('#machineDeleteModal').modal('hide');
            $.ajax({
                type: 'POST',
                url: '/machine_edit.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        machinetable.draw(false);
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        });

        $.machinedetil = function (id) {
            var param = {};
            param.id = id;
            $.submitFormPost('/machine_detail.do', param, 'POST');
        };
    });
</script>

